// AdminViewController.java
package com.neusoft.nepc.controller;

import com.neusoft.nepa.UserLogin;
import com.neusoft.nepa.entity.Admin;
import com.neusoft.nepa.entity.AdminData;
import com.neusoft.nepa.entity.AdminDataDTO;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class AdminViewController {
    @FXML
    private ImageView imageView;
    @FXML
    private Button btn1;
    @FXML
    private TableView<Task> taskTableView;
    @FXML
    private TableColumn<Task, Integer> idColumn;
    @FXML
    private TableColumn<Task, String> regionColumn;
    @FXML
    private TableColumn<Task, String> dateColumn;

    @FXML
    private TableColumn<Task, Void> actionColumn;

    private ObservableList<Task> taskData = FXCollections.observableArrayList();
    private String username;

    @FXML
    public void initialize() throws IOException {
        this.username = UserLoginViewController.currentLoggedInUserId;
        Image image = new Image("com/neusoft/nepc/Image/envi2.jpg");
        imageView.setImage(image);
        imageView.setPreserveRatio(false);
        imageView.setSmooth(true);

        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        regionColumn.setCellValueFactory(new PropertyValueFactory<>("region"));
        dateColumn.setCellValueFactory(new PropertyValueFactory<>("date"));


        actionColumn.setCellFactory(param -> new TableCell<>() {
            private final Button actionButton = new Button("去检测");

            {
                actionButton.setOnAction(event -> {
                    Task task = getTableView().getItems().get(getIndex());
                    handleInspectAction(task);
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    setGraphic(actionButton);
                }
            }
        });

        loadSampleData();
        taskTableView.setItems(taskData);
    }

    private void loadSampleData() throws IOException {
        UserLogin userLogin = new UserLogin();
        int i=0;
        ArrayList<String> temp = new ArrayList<>();
        temp.add("一");
        temp.add("二");
        temp.add("三");
        temp.add("四");
        temp.add("五");
        temp.add("六");
        for (AdminDataDTO infoUnit : userLogin.getArrangedData().getOrDefault(username, new ArrayList<>())) {
            i++;
            //province city aqi time detailed_time name info
            if (true) {
                taskData.add(new Task(i, infoUnit.getWithinProvince() + "__" + infoUnit.getWithinCity(), infoUnit.getFeedbackTime(), "某监督员", "5G", infoUnit.getFeedbackName(), infoUnit.getPreviewIndex(), infoUnit.getInfo()));
            }
        };
    }

    private void handleInspectAction(Task task) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            URL url = getClass().getResource("../view/TestView.fxml");
            fxmlLoader.setLocation(url);
            Parent root = fxmlLoader.load();

            TestViewController controller = fxmlLoader.getController();
            controller.initData(task);

            Stage stage = (Stage) taskTableView.getScene().getWindow();
            stage.setScene(new Scene(root));
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void back() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("../view/UserLoginView.fxml"));
        Parent registerRoot = fxmlLoader.load();

        Stage stage = (Stage) (btn1.getScene().getWindow());
        Scene registerScene = new Scene(registerRoot);
        stage.setScene(registerScene);
        stage.show();
    }

    public static class Task {
        private final int id;
        private final String region;
        private final String date;
        private final String username;
        private final String network;
        private final String details;
        private final String estimatedLevel;
        private final String description;

        public Task(int id, String region, String date,
                    String username, String network, String details,
                    String estimatedLevel, String description) {
            this.id = id;
            this.region = region;
            this.date = date;
            this.username = username;
            this.network = network;
            this.details = details;
            this.estimatedLevel = estimatedLevel;
            this.description = description;
        }

        public int getId() { return id; }
        public String getRegion() { return region; }
        public String getDate() { return date; }
        public String getUsername() { return username; }
        public String getNetwork() { return network; }
        public String getDetails() { return details; }
        public String getEstimatedLevel() { return estimatedLevel; }
        public String getDescription() { return description; }
    }
}